﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using CuaHangMayTinh.BusinessEntities;
using CuaHangMayTinh.BusinessLogicLayer;
using CuaHangMayTinh.Utilities;
using System.Threading;

namespace CuaHangMayTinh.PresentationLayer
{
    /// <summary>
    /// hiển thị thông tin , them, xóa, sửa, cập nhật khách hàng
    /// </summary>
    public partial class FormKhachHang : Form,IFormTV<KhachHangEntity>
    {
        /// <summary>
        /// chứa nội dung text lỗi
        /// </summary>
        private string Text = string.Empty;

        /// <summary>
        /// tables chua danh sach entity
        /// </summary>
        public DataTable Table { get; set; }
        /// <summary>
        /// danh sách khách hàng
        /// </summary>
        private List<KhachHangEntity> khachHangEntities; 
        /// <summary>
        /// phương thức khởi tạo
        /// </summary>
        public FormKhachHang()
        {
            Thread t = new Thread(() => (new Splash.Splash1()).ShowDialog());
            t.Start();
            Thread.Sleep(500);
            InitializeComponent();
            khachHangEntities = new List<KhachHangEntity>();
            LoadListView();
            t.Abort();
        }

        public FormKhachHang(bool[] so)
            : this()
        {
            if (so[0])
            {
                toolStripBtn_timkiem.Enabled = true;
            }
            if (so[1])
            {
                toolStripBtn_Them.Enabled = true;
                themToolStripMenuItem.Enabled = true;
            }
            if (so[2])
            {
                toolStripBtn_Xoa.Enabled = true;
                xoaToolStripMenuItem.Enabled = true;
            }
            if (so[3])
            {
                toolStripBtn_sua.Enabled = true;
                suaToolStripMenuItem.Enabled = true;
            }
        }

        /// <summary>
        /// bắt sự kiện thêm click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void btn_them_click(object sender, EventArgs e)
        {
            var formkhac = new FormKhacHangThem();
                        if(formkhac.ShowDialog()==DialogResult.OK)
                            LoadListView();
        }

        /// <summary>
        /// bắt sự kiện click của nút xóa
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void btn_xoa_click(object sender, EventArgs e)
        {
            if (dataGridViewX_Select.SelectedRows.Count > 0)
            {
                int i = 0;
                var c = MessageBox.Show("Bạn muốn xóa hay không", "Thông báo", MessageBoxButtons.YesNo,
                    MessageBoxIcon.Warning);
                if (c == DialogResult.Yes)
                {
                    foreach (DataGridViewRow row in dataGridViewX_Select.SelectedRows)
                    {
                        if (KhachHangManager.Delete((int)row.Cells[0].Value,ref Text))
                        {
                            i++;
                        }
                        else
                        {
                            MessageBox.Show(Text);
                        }
                    }
                    MessageBox.Show("Xóa thành công " + i + " khách hàng!");
                    LoadListView();
                }
            }
        }

        /// <summary>
        /// bắt sự kiện click của nút sửa
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void btn_sua_click(object sender, EventArgs e)
        {
            if (dataGridViewX_Select.SelectedRows.Count > 0)
            {
                var row = dataGridViewX_Select.SelectedRows[0];
                var entity = new KhachHangEntity()
                {
                    MaKH = (int)row.Cells[0].Value,
                    CMND_MaSoThue = (string)row.Cells[2].Value,
                    DiaChi = (string)row.Cells[3].Value,
                    Email = (string)row.Cells[5].Value,
                    SoDT = (string)row.Cells[4].Value,
                    TenKH = (string)row.Cells[1].Value

                };
                FormKhacHangThem khacHangThem = new FormKhacHangThem(entity);
                if (khacHangThem.ShowDialog() == DialogResult.OK)
                    LoadListView();
            }
        }

        /// <summary>
        /// bắt sự kiện click của nút tìm kiếm
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void btn_timkiem_click(object sender, EventArgs e)
        {
            FormKhachHangTimKiem formKhachHangTimKiem = new FormKhachHangTimKiem(Table);
            formKhachHangTimKiem.ShowDialog();
        }

        /// <summary>
        /// sự kiện thay đổi text của textbox 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStriptxt_timnhanh_TextChanged(object sender, EventArgs e)
        {
            string text = (sender as ToolStripTextBox).Text;
            if (text != "")
            {
                DataTable dt = Table;
                var table2 =dt.AsEnumerable().Where(x =>DataUtil.convertToUnSign3(x.Field<string>("Tên khách hàng").ToLower())
                                    .StartsWith(DataUtil.convertToUnSign3(text.ToLower())));
                LoadListView(table2.Any() ? table2.CopyToDataTable() : null);
            }
            else
            {
                LoadListView(Table);
            }
        }
        
        /// <summary>
        /// sựa kiện load của form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormKhachHang_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// load dữ liệu lên datagridview bằng findall
        /// </summary>
        public void LoadListView()
        {
            Table = DataUtil.ChangeColumn(KhachHangManager.Find_All());
            dataGridViewX_Select.DataSource = Table;
            if (dataGridViewX_Select.Columns.Count > 0)
            {
                dataGridViewX_Select.Columns[0].Visible = false;
            }
        }

        /// <summary>
        /// load dữ liệu lên datagird từ table dt
        /// </summary>
        /// <param name="dt"></param>
        public void LoadListView(DataTable dt)
        {
            dataGridViewX_Select.DataSource = dt;
            if (dataGridViewX_Select.Columns.Count > 0)
            {
                dataGridViewX_Select.Columns[0].Visible = false;
            }
        }

        
        /// <summary>
        /// thêm của menustrip
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void themToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btn_them_click(sender, e);
        }

        /// <summary>
        /// xóa của menustrip
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xoaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btn_xoa_click(sender,e);
        }

        /// <summary>
        /// sửa của menustrip
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void suaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btn_sua_click(sender, e);
        }
    }
}
